<template>
  <div class="home">
    <!-- 头部 -->
    <div class="tou">
      <div class="l" @click="$router.go(-1)">返回</div>
      <div class="c">
        <!-- <input type="text" placeholder="请输入内容" class="inp" /> -->
        <van-search
          v-model="value"
          placeholder="请输入搜索关键词"
          @update:model-value="change"
        />
      </div>
      <div class="r" @click="$router.go(-1)" v-if="show">取消</div>
      <div class="r" v-else @click="sou">搜索</div>
    </div>
    <!-- 历史搜索 -->
    <div class="list">
      <div class="listt">
        <div class="l">历史搜索</div>
        <div class="r"><van-icon name="delete-o" @click="del"/></div>
      </div>
      <!-- 内容 -->
      <van-tag
        round
        type="default"
        size="medium"
        v-for="(item, index) in store.historyArr"
        :key="index"
        @click="ss(item)"
        >{{ item }}</van-tag
      >
    </div>
  </div>
</template>

<script>
import { toRefs, reactive, onMounted, watch, watchEffect } from 'vue'
import { Toast } from 'vant'
import { useRouter } from 'vue-router'
import { getSearchApi } from '../http/api.js'
import { useCounterStore } from '@/stores/counter'
export default {
  setup(props) {
    const store = useCounterStore()
    const router = useRouter()
    const data = reactive({
      value: '',
      show: true
    })
    // 搜索框的值
    const change = (e) => {
      console.log('搜索框的值', e)
      data.value = e
    }
    // 点击搜索按钮
    const sou = () => {
      console.log(1)
      console.log(data.value)
      if (data.value == '') {
        Toast.fail('请输入内容哦')
      } else {
        let index = store.historyArr.indexOf(data.value)
        if (index > -1) {
          store.historyArr.splice(index, 1)
        } else {
          if (store.historyArr.length >= 6) {
            store.historyArr.pop()
          }
        }
        store.addHistory(data.value)
      }
      router.push({ path: '/sou', query: { title: data.value } })
      data.value = ''
    }
    // 监听搜索框的值
    watchEffect(() => {
      console.log(1)
      console.log('value:', data.value)
      if (data.value == '') {
        data.show = true
      } else {
        data.show = false
      }
    })
    // 点击赋值
    const ss = (item) => {
      data.value = item
    }
    // 清空历史记录
    const del=()=>{
      store.delm()
    }
    return {
      ...toRefs(data),
      change,
      sou,
      store,
      ss,
      del
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 5px 20px;
  .tou {
    width: 100%;
    height: 100px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eee;
    .l,
    .r {
      width: 10%;
    }
    .c {
      width: 72%;
      height: 60px;
      margin-top: -55px;
      .inp {
        width: 100%;
        height: 100%;
        border: 1px solid #e4e7ed;
        border-radius: 60px;
        outline: 0;
        box-sizing: border-box;
        padding-left: 50px;
        background: url(../../public/3.jpg) no-repeat 8px;
        background-size: 35px;
        background-color: #e4e7ed;
      }
    }
  }
  .list {
    width: 100%;
    box-sizing: border-box;
    margin-top: 30px;
    .listt {
      width: 100%;
      height: 60px;
      justify-content: space-between;
      align-content: center;
      display: flex;
    }
    .van-tag {
      margin: 10px;
    }
  }
}
</style>
